package com.micro.controller;

import com.micro.entity.RequestHeader;
import com.micro.form.SettingsForm;
import com.micro.global.ConfigInfo;
import com.micro.global.MainTaskTreadPool;
import com.micro.utils.SeriaUtil;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import org.apache.commons.lang3.StringUtils;

import java.net.URL;
import java.util.ResourceBundle;
import java.util.StringJoiner;


/**
 * @author dufen
 * @date 2021-10-16 11:18:25
 * @description
 */
public class SettingsController implements Initializable {
    @FXML
    private TextField taskCoreSize;
    @FXML
    private TextField downCoreSize;
    @FXML
    private TextField tries;
    @FXML
    private TextArea textArea;

    public void save(ActionEvent actionEvent) {
        if (ConfigInfo.getConfigInfo() != null) {
            ConfigInfo.getConfigInfo().setTaskCoreSize(StringUtils.isNotBlank(taskCoreSize.getText()) ? Integer.parseInt(taskCoreSize.getText()) : 5);
            ConfigInfo.getConfigInfo().setDownCoreSize(StringUtils.isNotBlank(downCoreSize.getText()) ? Integer.parseInt(downCoreSize.getText()) : 8);
            ConfigInfo.getConfigInfo().setTries(StringUtils.isNotBlank(tries.getText()) ? Integer.parseInt(tries.getText()) : 255);
        } else {
            com.micro.entity.ConfigInfo configInfo = new com.micro.entity.ConfigInfo();
            configInfo.setTaskCoreSize(StringUtils.isNotBlank(taskCoreSize.getText()) ? Integer.parseInt(taskCoreSize.getText()) : 5);
            configInfo.setDownCoreSize(StringUtils.isNotBlank(downCoreSize.getText()) ? Integer.parseInt(downCoreSize.getText()) : 8);
            configInfo.setTries(StringUtils.isNotBlank(tries.getText()) ? Integer.parseInt(tries.getText()) : 255);
            RequestHeader requestHeader = new RequestHeader.Build().build();
            configInfo.setRequestHeader(requestHeader);
            ConfigInfo.setConfigInfo(configInfo);
        }
        MainTaskTreadPool.getINSTANCE().setCorePoolSize(Integer.parseInt(taskCoreSize.getText()));
        SeriaUtil.writeObjectToFile(ConfigInfo.getConfigInfo(),"configInfo.obj");
        SettingsForm.hide();
    }

    public RequestHeader resolveHeader() {
        RequestHeader requestHeader = new RequestHeader.Build().build();
        if (StringUtils.isNotBlank(textArea.getText())) {
            String text = textArea.getText();
            String[] lines = text.split("\n");
            if (lines.length > 0) {
                for (String line : lines) {
                    String[] split = line.split(":");
                    if (split.length == 2) {
                        requestHeader.addHeader(split[0], split[1]);
                    }
                    if (split.length > 2) {
                        String key = split[0];
                        StringJoiner stringJoiner = new StringJoiner(":");
                        for (int i = 1; i < split.length; i++) {
                            stringJoiner.add(split[i]);
                        }
                        requestHeader.addHeader(key, stringJoiner.toString());
                    }
                }
            }
        }
        return requestHeader;
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        //设色背景
        textArea.setStyle("-fx-control-inner-background:#3C3F41;");
        taskCoreSize.setText(String.valueOf(ConfigInfo.getConfigInfo().getTaskCoreSize()));
        downCoreSize.setText(String.valueOf(ConfigInfo.getConfigInfo().getDownCoreSize()));
        tries.setText(String.valueOf(ConfigInfo.getConfigInfo().getTries()));
    }
}
